package com.css.security.core.social.config;

import com.css.security.core.config.CssSpringSocialConfigurer;
import com.css.common.security.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

/**
 * SocialConfig
 * 社交登录配置主类
 *
 * @author hanyx
 * @date 2019/04/14
 */
@Configuration
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired(required = false)
    private ConnectionSignUp connectionSignUp;

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        /**
         * Encryptors.noOpText() 暂时不做加解密
         */
        JdbcUsersConnectionRepository usersConnectionRepository = new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
        usersConnectionRepository.setTablePrefix("CSS_");
        // 第三方登录默认注册用户
        if (connectionSignUp != null) {
            usersConnectionRepository.setConnectionSignUp(connectionSignUp);
        }
        return usersConnectionRepository;
    }

    /**
     * 把 SocialAuthenticationFilter,加到Spring security 过滤连中
     * filterProcessesUrl : 社交登录过滤器拦截路径 /auth
     *
     * @return
     */
    @Bean
    public SpringSocialConfigurer cssSpringSocialConfigurer() {
        String filterProcessesUrl = securityProperties.getSocial().getFilterProcessesUrl();
        CssSpringSocialConfigurer cssSpringSocialConfigurer = new CssSpringSocialConfigurer(filterProcessesUrl);
        cssSpringSocialConfigurer.signupUrl(securityProperties.getBrowser().getSignUpUrl());
        return cssSpringSocialConfigurer;
    }

    /**
     * 1.注册过程中拿到SpringSocial信息
     * 2.注册完成把业务数据传给SpringSocial
     *
     * @param connectionFactoryLocator springboot自动注入
     * @return
     */
    @Bean
    public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator connectionFactoryLocator) {
        return new ProviderSignInUtils(connectionFactoryLocator, getUsersConnectionRepository(connectionFactoryLocator));
    }
}
